import 'dart:math';

import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';

class SuccessIndicator extends AnimatedWidget {
  const SuccessIndicator({
    required AnimationController controller,
    super.key,
  }) : super(listenable: controller);

  AnimationController get _controller => listenable as AnimationController;

  @override
  Widget build(BuildContext context) {
    const opacityDelimiter = 0.5;

    late double opacity;
    if (_controller.value < opacityDelimiter) {
      opacity = 1.0;
    } else {
      opacity = 1.0 - (_controller.value - opacityDelimiter) / (1.0 - opacityDelimiter);
    }

    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Opacity(
          opacity: opacity,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Container(
              color: CoreTheme.of(context).successIndicatorBackgroundColor,
              width: 150,
              height: 150,
              child: CustomPaint(
                painter: _CheckMarkPainter(
                  fraction: min(1.0, _controller.value / opacityDelimiter),
                  color: CoreTheme.of(context).successIndicatorColor,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _CheckMarkPainter extends CustomPainter {
  const _CheckMarkPainter({
    required this.fraction,
    required this.color,
  });

  final double fraction;
  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 6.5
      ..strokeCap = StrokeCap.round;

    final horizontalPadding = size.width / 5;
    final verticalPadding = size.height / 6 + 2 * horizontalPadding / 3;

    const fractionDelimiter = 1 / 3;
    final firstLineFraction = min(fraction / fractionDelimiter, 1.0);
    final secondLineFraction = max(0.0, (fraction - fractionDelimiter) / (1.0 - fractionDelimiter));

    if (firstLineFraction > 0) {
      canvas.drawLine(
        Offset(horizontalPadding, size.height / 2),
        Offset(
          horizontalPadding + (size.height / 2 - verticalPadding) * firstLineFraction,
          size.height / 2 + (size.height / 2 - verticalPadding) * firstLineFraction,
        ),
        paint,
      );
    }

    if (secondLineFraction > 0) {
      canvas.drawLine(
        Offset(
          horizontalPadding + size.height / 2 - verticalPadding,
          size.height - verticalPadding,
        ),
        Offset(
          horizontalPadding +
              size.height / 2 -
              verticalPadding +
              (size.height - 2 * verticalPadding) * secondLineFraction,
          size.height - verticalPadding + (2 * verticalPadding - size.height) * secondLineFraction,
        ),
        paint,
      );
    }
  }

  @override
  bool shouldRepaint(_CheckMarkPainter oldDelegate) {
    return fraction != oldDelegate.fraction;
  }
}
